package com.xncoding.minispring.Concrete;

import com.xncoding.minispring.inter.IObserver;
import com.xncoding.minispring.inter.ISubject;

import java.util.ArrayList;
import java.util.List;

/**
 *  具体被观察者
 */
public class ConcreteSubject implements ISubject {

    private List<IObserver> observers = new ArrayList<>();

    @Override
    public boolean attach(IObserver observer) {
        return this.observers.add(observer);
    }

    @Override
    public boolean detach(IObserver observer) {
        return this.observers.remove(observer);
    }

    @Override
    public void notify(String event) {
        System.out.println("被观察者: 数据变更 = " + event);
        for (IObserver observer : this.observers) {
            observer.update(event);
        }
    }
}